package com.example.aslongastoday.ui.mine;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.aslongastoday.util.CloudUtil;
import com.example.aslongastoday.util.MD5Utils;

import com.example.aslongastoday.MainActivity;
import com.example.aslongastoday.R;

import java.util.concurrent.ExecutionException;

public class LoginFragment extends Fragment {
    private View view;
    private Button login;
    private MineFragment Fragment;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    private EditText et_user_name,et_psw;
    private String userName,psw,spPsw;
    private TextView tv_register,tv_find_psw;
    private RegisterFragment registerFragment;
    private ImageView iv_back;

    private ForgotFragment forgotFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_login, container, false);
        login = view.findViewById(R.id.btn_login);

        iv_back = view.findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            private FragmentManager fragmentManager;
            @Override
            public void onClick(View v) {
                //退出fragment
                fragmentManager = getActivity().getSupportFragmentManager();
                fragmentManager.popBackStack();
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                et_user_name= view.findViewById(R.id.et_user_name);
                et_psw=view.findViewById(R.id.et_psw);
                userName=et_user_name.getText().toString().trim();
                psw=et_psw.getText().toString().trim();
                String md5Psw= MD5Utils.md5(psw);


                if (remoteLogin(userName,md5Psw)) {
                    Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                    saveLoginStatus(true, userName);
                    Intent intent=new Intent(getContext(), MainActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(getContext(), "登录失败", Toast.LENGTH_SHORT).show();
                }




//                //对当前用户输入的密码进行MD5加密再进行比对判断, MD5Utils.md5( ); psw 进行加密判断是否一致
//
//                // md5Psw ; spPsw 为 根据从SharedPreferences中用户名读取密码
//                // 定义方法 readPsw为了读取用户名，得到密码
//                spPsw=readPsw(userName);
//
//                if(TextUtils.isEmpty(userName)){
//                    Toast.makeText(getContext(), "请输入用户名", Toast.LENGTH_SHORT).show();
//                    return;
//                }else if(TextUtils.isEmpty(psw)){
//                    Toast.makeText(getContext(), "请输入密码", Toast.LENGTH_SHORT).show();
//                    return;
//                }else if(md5Psw.equals(spPsw)){
//                    //一致登录成功
//                    Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
//                    saveLoginStatus(true, userName);
//                    Intent intent=new Intent(getContext(), MainActivity.class);
//                    startActivity(intent);
//                    return;
//                }else if((spPsw!=null&&!TextUtils.isEmpty(spPsw)&&!md5Psw.equals(spPsw))){
//                    Toast.makeText(getContext(), "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();
//                    return;
//                }else{
//                    Toast.makeText(getContext(), "此用户名不存在", Toast.LENGTH_SHORT).show();
//                }
            }
        });

        tv_find_psw = view.findViewById(R.id.tv_find_psw);
        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                forgotFragment= new ForgotFragment();
                fragmentManager = getActivity().getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                //跳转功能
                fragmentTransaction.add(R.id.container,forgotFragment);
                fragmentTransaction.setPrimaryNavigationFragment(forgotFragment);
                //可返回上一fragment
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

        tv_register = view.findViewById(R.id.tv_register);
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerFragment = new RegisterFragment();
                fragmentManager = getActivity().getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                //跳转功能
                fragmentTransaction.add(R.id.container,registerFragment);
                fragmentTransaction.setPrimaryNavigationFragment(registerFragment);
                //可返回上一fragment
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        return view;
    }

    private boolean remoteLogin(String userName,String password) {
        try {
            CloudUtil.Login(userName,password);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     *从SharedPreferences中根据用户名读取密码
     */
    private String readPsw(String userName){
        //getSharedPreferences("loginInfo",MODE_PRIVATE);
        //"loginInfo",mode_private; MODE_PRIVATE表示可以继续写入
        SharedPreferences sp = getContext().getSharedPreferences("loginInfo", MODE_PRIVATE);
        //sp.getString() userName, "";
        return sp.getString(userName , "");
    }

    private void saveLoginStatus(boolean status, String userName){
        //saveLoginStatus(true, userName);
        //loginInfo表示文件名  SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences sp=getContext().getSharedPreferences("loginInfo", MODE_PRIVATE);
        //获取编辑器
        SharedPreferences.Editor editor=sp.edit();
        //存入boolean类型的登录状态
        editor.putBoolean("isLogin", status);
        //存入登录状态时的用户名
        editor.putString("loginUserName", userName);
        //提交修改
        editor.commit();
    }
}